var app = getApp();
//获得

var MyRequest = require("../../utils/MyRequest.js");
var Global = require("../../Global.js");


Page({
  //页面初始化的数据
  data: {
    userinfo: "",
    couponSize: 0
  },
  onLoad: function () {
    //获取数据

  },

  //页面初次渲染
  onReady: function () {

  },
  //页面显示
  onShow: function () {
    this.setData({
      userinfo: Global.getUserStorage()
    })
    this.Presenter.getCoupons(this);
    this.Presenter.init(this);
  },
  //页面隐藏
  onHide: function () {
  },
  //页面删除
  onUnload: function () {
  },
  //请求数据者
  Presenter: {
    Refresh: function (that) {
      var data = { "action": "info", id: that.data.userinfo.id };
      MyRequest.MyGetWithRefresh("UserInfo", that, function (result) {
        if (result.data.success) {
          Global.setUserStorage(result.data.data);
          that.setData({
            userinfo: result.data.data
          })
        }
      }, data)
    },
    init: function (that) {
      var data = { "action": "info", id: that.data.userinfo.id };
      MyRequest.MyGet("UserInfo", that, function (result) {
        if (result.data.success) {
          Global.setUserStorage(result.data.data);
          that.setData({
            userinfo: result.data.data
          })
          console.log(result.data.data)
        }
      }, data)
    },
    getCoupons: function (that) {
      var user = Global.getUserStorage();
      if (user) {
        var data = { userId: user.id };
        MyRequest.MyGet("CouponCount", that, that.Binder.setCoupon, data);
      }
    }
  },
  Binder: {
    setCoupon: function (result, that) {
      that.setData({
        couponSize: result.data.data
      });
    }
  },

  tologin: function () {

    if (!Global.getUserStorage()) {

      wx.navigateTo({
        url: '/pages/login/login'
      })
    }
  },
  exit: function () {
    var that = this;
    wx.showModal({
      title: '注销提示',
      content: '您确定要退出吗？',
      success: function (res) {
        if (res.confirm) {
          Global.clearUserStorage();
          that.setData({
            userinfo: ""
          });
        }
      }
    })



  },

  //用户下拉
  onPullDownRefresh: function () {

    if (this.data.userinfo == "") {
      wx.stopPullDownRefresh();
      return;
    }
    this.Presenter.Refresh(this);
  },
  toCartPage: function (event) {
    if (!Global.getUserStorage()) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
      return;

    }
    wx.navigateTo({
      url: '/pages/cart/cart'
    });
  },
  toOrder: function (event) {
    if (!Global.getUserStorage()) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
      return;

    }
    wx.navigateTo({
      url: '/pages/orderList/orderList'
    });
  },
  toAddress: function () {
    if (!Global.getUserStorage()) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
      return;
    }

    wx.navigateTo({
      url: '/pages/address/address'
    })
  },
  toTransactionDetail: function () {
    if (!Global.getUserStorage()) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/transactionDetails/transactionDetails'
    })
  },

  call: function () {
    wx.makePhoneCall({
      phoneNumber: '4006059144',
      success: function (res) {
        // success
      }
    })
  },
  suggestion: function () {
    wx.navigateTo({
      url: '/pages/suggestion/suggestion'
    })
  },
  toCoupon: function () {
    if (!Global.getUserStorage()) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/coupon/coupon'
    })
  },
  toBalance: function () {
    if (!Global.getUserStorage()) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
      return;
    }
    var params = "?balance=";
    params += this.data.userinfo.money;
    wx.navigateTo({
      url: '/pages/balance/balance' + params
    })


  }
})
